﻿var hidecompatchcolume = true;
function IsWhat(value) {
    if (IsChinese(value))
        return 0;
    else if (IsNumber(value))
        return 4;
    else
        return 2;

}
function IsChinese(value) {
    value = value.charAt(0);
    var escape_v = escape(value);
    if (escape_v.length == (value.length) * 6 || value == "\r" || value == "\n")
        return true;
    else
        return false;

}
function IsNumber(value) {
    var num = parseInt(value);
    if (num == 0 || num)
        return true;
    else
        return false;

}
function Trim(value) {
    return value.replace(/^\s+|\s+$/g, "");
}
var isIE6 = document.all && ([/MSIE (\d)\.0/i.exec(navigator.userAgent.toLowerCase())][0][1] == 6);
function Bind(object, fun) {
    var args = Array.prototype.slice.call(arguments).slice(2);
    return function () {
        return fun.apply(object, args.concat(Array.prototype.slice.call(arguments)));
    }
}
function BindAsEventListener(object, fun) {
    var args = Array.prototype.slice.call(arguments).slice(2);
    return function (e) {
        return fun.call(object, (e || window.event), args);
    }
}
window.attachEventHandle = function (elem, eventName, func) {
    if (window.attachEvent) {
        elem.attachEvent('on' + eventName, func);
    } else if (window.addEventListener) {
        elem.addEventListener(eventName, func, false);
    }
};
window.detachEventHandle = function (elem, eventName, func) {
    if (window.detachEvent) {
        elem.detachEvent('on' + eventName, func);
    } else if (window.removeEventListener) {
        elem.removeEventListener(eventName, func, false);
    }
};
function getPosition(elem) {
    var x = 0,
    y = 0;
    while (elem) {
        x += elem.offsetLeft || 0;
        y += elem.offsetTop || 0;
        elem = elem.offsetParent;
    }
    return {
        x: x,
        y: y
    };

}
function searchPicker(txtEnterID, dataArray, pickerType, filledCode, changeTop, changeValue, options) {
    this.enterValue = "";
    this.enterID = txtEnterID;
    this.show_hide = "hide";
    this.focus = true;
    this.Time = true;
    this.Trs = null;
    this.Rows = null;
    this.dataArray = dataArray;
    this.pickerType = pickerType;
    this.options = options;
    this.inDrawTable = false;
    this.timerFsInDrawTable = null;
    this.divHeight = 250;
}
searchPicker.prototype = {
    fsInit: function () {
        this.Dom = document.getElementById(this.enterID);
        this.Div = document.body.insertBefore(document.createElement('div'), document.body.childNodes[0]);
        this.Div.className = 'searchPicker_listDiv';
        with (this.Div.style) {
            zIndex = 999999;
            position = 'absolute';
            left = getPosition(this.Dom).x + 'px';
            top = getPosition(this.Dom).y + this.Dom.offsetHeight + 'px';
        }
        if (hidecompatchcolume) {
            this.Div.style.width = "260px";
        }
        attachEventHandle(this.Dom, "focus", Bind(this, this.fsClearValue));
        attachEventHandle(this.Dom, "blur", Bind(this, this.fsBlur));
        attachEventHandle(this.Dom, "keydown", BindAsEventListener(this, this.fsKeyDown));
        attachEventHandle(this.Dom, "keyup", BindAsEventListener(this, this.fsKeyUp));
        this.timerCheckIsDisplay = window.setInterval(Bind(this, this.checkIsDisplay), 300);
        if (this.changeTop == null) {
            interval = window.setInterval(Bind(this, this.fsChangeTop), 20);
        }

    },
    checkIsDisplay: function () {
        try {
            var isDisplay = true;
            var el = this.Dom;
            alert(thid.Div);
            if (this.Div.style.display == 'none') {
                return;
            }
            while (el && el.tagName.toLowerCase() != 'body') {
                el = el.parentNode;
                if (!el || (el.style && el.style.display == 'none')) {
                    isDisplay = false;
                    break;
                }
            }
            if (!isDisplay) {
                this.fsHideDiv();
                if (this.timerCheckIsDisplay) {
                    window.clearInterval(this.timerCheckIsDisplay);
                }
            }

        } catch (e) { }

    },
    fsClearValue: function (e) {
        switch (this.pickerType) {
            case "fund":
                if (this.Dom.value == "基金代码/拼音简称/基金简称")
                    this.Dom.value = "";
                break;
            case "fund2":
                if (this.Dom.value == "基金代码/拼音简称/基金简称")
                    this.Dom.value = "";
                break;
            case "user":
                if (this.Dom.value == "请输入客户姓名")
                    this.Dom.value = "";
                break;
            case "FundHY":
                if (this.Dom.value == "请选择重仓股票")
                    this.Dom.value = "";
                break;

        }

    },
    fsSetDefaultValue: function () {
        switch (this.pickerType) {
            case "fund":
                if (this.Dom.value == "") {
                    this.Dom.value = "基金代码/拼音简称/基金简称";
                }
                break;
            case "fund2":
                if (this.Dom.value == "") {
                    this.Dom.value = "基金代码/拼音简称/基金简称";
                }
                break;
            case "user":
                if (this.Dom.value == "") {
                    this.Dom.value = "请输入客户姓名";
                }
                break;
            case "FundHY":
                if (this.Dom.value == "") {
                    this.Dom.value = "请选择重仓股票";
                }
                break;

        }

    },
    fsBlur: function () {
        window.setTimeout(Bind(this, this.fsSetDefaultValue), 100);
    },
    fsFill: function (code, name) {
        this.Dom.title = code;
        this.Dom.setAttribute("code", code);
        this.Dom.value = name;
        this.Dom.blur();
        this.fsHideDiv();
        this.enterValue = "";
        if (this.fsFilledCode) {
            this.fsFilledCode();
        }
        if (this.fsChangeValue) {
            this.fsChangeValue();
        }
        if (typeof (this.options) != 'undefined' && this.options.length > 0) {
            top.location.href = this.options + this.Dom.getAttribute("code");
        }

    },
    fsMouseOver: function (e, i) {
        if (e != "div") {
            this.Trs[this.Rows].style.backgroundColor = "#fff";
            this.Trs[this.Rows].style.color = "#333";
            e.style.backgroundColor = "#cedfe9";
            e.style.color = "#333";
            if (i == undefined)
                this.Rows = parseInt(e.title.split("|")[2]);

        }
        this.show_hide = "show";

    },
    fsMouseOut: function (e) {
        if (e != "div") {
            e.style.backgroundColor = "#fff";
            e.style.color = "#333";
        }
        this.show_hide = "hide";

    },
    fsKeyDown: function (e) {

        if (e.keyCode == 38) {
            if ((this.Rows > 0) && this.Rows) {
                var text;
                if (this.Rows - 1 > 0) {
                    text = this.Trs[this.Rows - 1].innerText || this.Trs[this.Rows - 1].textContent;
                    text = Trim(text);
                } else {
                    return;
                }
                if (text != "选项代码名称" && text != "选项功能拼音") {
                    this.fsMouseOver(this.Trs[this.Rows - 1], 0);
                    this.fsScroll(this.Trs[this.Rows - 1], -1, "up");
                    this.Rows -= 1;
                } else if (this.Rows - 2 > 0) {
                    this.fsMouseOver(this.Trs[this.Rows - 2], +2);
                    this.fsScroll(this.Trs[this.Rows - 2], -2, "up");
                    this.Rows -= 2;
                }
                this.show_hide = "hide";

            }

        } else if (e.keyCode == 40) {
            if ((this.Trs.length > this.Rows) && this.Rows) {
                var text;
                if (this.Rows + 1 < this.Trs.length) {
                    text = this.Trs[this.Rows + 1].innerText || this.Trs[this.Rows + 1].textContent;
                    text = Trim(text);
                } else {
                    return;
                }
                if (text != "选项代码名称" && text != "选项功能拼音") {
                    this.fsMouseOver(this.Trs[this.Rows + 1], 1);
                    this.fsScroll(this.Trs[this.Rows + 1], 1, "down");
                    this.Rows += 1;
                }
                else if (this.Trs.length > this.Rows + 2) {
                    this.fsMouseOver(this.Trs[this.Rows + 2], -2);
                    this.fsScroll(this.Trs[this.Rows + 2], 2, "down");
                    this.Rows += 2;
                }
                this.show_hide = "hide";

            }

        } else if (e.keyCode == 13) {
            if (this.show_hide == "show" || this.Dom.title == "") {
                var Title = this.Trs[this.Rows].title.split("|");
                if (Title) {
                    this.show_hide = "hide";
                    this.fsFill(Title[0], Title[1]);
                }
            }
        }

    },
    fsKeyUp: function (e) {
        if (e.keyCode == 38 || e.keyCode == 40 || e.keyCode == 13) {
            return;
        } else if (e.keyCode == 27) {
            window.event.keyCode = 0;
            window.event.returnValue = false;
        } else {
            this.fsBindSearch();
        }
    },
    fsScroll: function (e, i, direction) {
        var Num;
        var Number;
        if (window.event) {
            Num = 25;
        } else {
            Num = 25;
        }
        Number = this.Div.offsetHeight;
        var range = e.offsetTop - this.Div.scrollTop;
        if (direction == "up") {
            if (range <= 0) {
                this.Div.scrollTop += Num * i;
            }
        }
        else if (direction == "down") {
            if (range + 25 >= Number) {
                this.Div.scrollTop += Num * i;
            }
        }

    },
    fsBindSearch: function () {
        var tag = false;
        if ((((this.pickerType == "fund" || this.pickerType == "fund2") && this.Dom.value != "基金代码/拼音简称/基金简称")) && Trim(this.Dom.value) != "") {
            var value = Trim(this.Dom.value);
            for (var i = 0; i < this.dataArray.length; i++) {
                if (this.dataArray[i].length >= 2 && this.dataArray[i][0] == value) {
                    if (this.Dom.getAttribute("code") != this.dataArray[i][1]) {
                        tag = true;
                        this.Dom.title = this.dataArray[i][1];
                        this.Dom.setAttribute("code", this.dataArray[i][1]);
                        if (this.fsChangeValue) {
                            this.fsChangeValue();
                        }
                    }
                }
            }
        }
        if (!tag) {
            if (this.Dom.getAttribute("code") != "") {
                this.Dom.title = "";
                this.Dom.setAttribute("code", "");
            }
        }
        if (((this.pickerType == "user" && this.Dom.value != "请输入客户姓名") || ((this.pickerType == "fund" || this.pickerType == "fund2") && this.Dom.value != "基金代码/拼音简称/基金简称") || (this.pickerType == "FundHY" && this.Dom.value != "请选择重仓股票")) && (this.enterValue != this.Dom.value)) {
            this.Dom.value = Trim(this.Dom.value);
            this.fsGetSearchData(this.Dom.value);
            this.enterValue = this.Dom.value;
        }

    },
    fsGetSearchData: function (KeyWord) {
        if (!this.inDrawTable) {
            this.inDrawTable = true;
            if (this.pickerType == "fund" || this.pickerType == "fund2") {
                if (KeyWord == '') {
                    this.fsGetSearchDataTable(KeyWord, []);
                } else {
                    // this.fsGetSearchDataTable(KeyWord, this.dataArray);
                    var curindex = IsWhat(KeyWord);
                    Ext.Ajax.request({
                        url: "Default.aspx",
                        params: {
                            type: this.pickerType,
                            key: KeyWord,
                            curIndex: curindex
                        },
                        success: this.fsGetSuggestStockCodesCallBack.createDelegate(this, [KeyWord], true)
                    });

                }
            } else if (KeyWord == "") {
                this.fsGetSearchDataTable(KeyWord, []);
            } else {

                //临时对用户输入限制，搜索用户时启用
                if (this.pickerType == "user" && (IsNumber(KeyWord) && KeyWord.length > 3)) {
                    Ext.Ajax.request({
                        url: "Default.aspx",
                        params: {
                            type: this.pickerType,
                            key: KeyWord,
                            workno: document.getElementById("hdfCurWorkNo").value
                        },
                        success: this.fsGetSuggestStockCodesCallBack.createDelegate(this, [KeyWord], true)
                    });
                } else {
                    Ext.Ajax.request({
                        url: "Default.aspx",
                        params: {
                            type: this.pickerType,
                            key: KeyWord,
                            workno: document.getElementById("hdfCurWorkNo").value
                        },
                        success: this.fsGetSuggestStockCodesCallBack.createDelegate(this, [KeyWord], true)
                    });
                }

            }
        }
    },
    fsGetSuggestStockCodesCallBack: function (response, options, KeyWord) {
        if (response.responseText.indexOf('[') != -1) {
            this.dataArray = eval(response.responseText);
        } else if (response.responseText != '') {
            this.dataArray = [];
        }
        this.fsGetSearchDataTable(KeyWord, this.dataArray);

    },
    fsGetSearchDataTable: function (KeyWord, dataArrayList) {
        var fundTable = document.createElement('table');
        fundTable.cellpadding = '0';
        fundTable.cellspacing = '0';
      //  fundTable.width = '100%';
        fundTable.style.width = '100%';
        fundTable.style.borderCollapse = 'collapse';
        fundTable.className = 'searchPicker_titleCss';
        var tbody = document.createElement('tbody');
        fundTable.appendChild(tbody);
        var index = IsWhat(KeyWord);
        var tr1,
        tr2,
        tr3;
        if (index == 0) {
            tr1 = "40%";
            tr2 = "20%";
            tr3 = "40%";
        }
        else {
            tr1 = "30%";
            tr2 = "30%";
            tr3 = "40%";
        }
        var Row = 1;
        KeyWord = KeyWord.toLowerCase();
        Len = dataArrayList.length;
        var sequeace;
        if (Row == 1) sequeace = 0;
        else {
            sequeace = Row + 1;
            Row += 2;
        }
        for (var i = 0; i < Len; i++) {
            var code = dataArrayList[i][1];
            var name = dataArrayList[i][0];
            var Title = dataArrayList[i][1] + "|" + name;
            var item = dataArrayList[i][index];
            var subKW = item.substr(0, KeyWord.length);
            if (subKW == KeyWord && subKW != -1) {
                var select = "<span style='color:Red'>" + subKW + "</span>"
                select += item.substr(KeyWord.length, item.length - KeyWord.length);
                var tr = document.createElement('tr');
                tr.title = Title + "|" + Row;
                attachEventHandle(tr, 'click', Bind(this, this.fsFill, code, name));
                attachEventHandle(tr, 'mouseover', Bind(this, this.fsMouseOver, tr));
                attachEventHandle(tr, 'mouseout', Bind(this, this.fsMouseOut, tr));
                var td = document.createElement("td");
                td.innerHTML = "&nbsp;" + select;
                tr.appendChild(td);
                if (hidecompatchcolume) {
                    td.style.display = "none";
                }
                var td = document.createElement("td");
                if (hidecompatchcolume) {
                    td.innerHTML = "&nbsp;" + dataArrayList[i][4];
                } else {
                    td.innerHTML = dataArrayList[i][4];
                }
                tr.appendChild(td);
                if (this.pickerType == "user")
                    td.style.display = "none";
                var td = document.createElement("td");
                td.innerHTML = dataArrayList[i][0];
                tr.appendChild(td);
                tbody.appendChild(tr);
                Row++;

            }

        }
        if (tbody.childNodes.length > 0) {
            var tr = document.createElement('tr');
            tr.className = "header";
            var td = document.createElement("td");
            td.innerHTML = "&nbsp;选项";
            td.width = tr1;
            tr.appendChild(td);
            if (hidecompatchcolume) {
                td.style.display = "none";
            }
            var td = document.createElement("td");
            if (hidecompatchcolume) {
                td.innerHTML = "&nbsp;代码";
                td.width = "15%";
            } else {
                td.innerHTML = "代码";
                td.width = tr2;
            }
            tr.appendChild(td);
            if (this.pickerType == "user")
                td.style.display = "none";
            var td = document.createElement("td");
            td.innerHTML = this.pickerType == "user" ? "姓名" : "名称";
            td.width = tr3;
            tr.appendChild(td);
            tbody.insertBefore(tr, tbody.firstChild);

        }
        else {
            fundTable = null;
        }
        if (fundTable) {
            this.Div.innerHTML = '';
            this.Div.appendChild(fundTable);
            this.fsShowDiv();
            this.Trs = this.Div.getElementsByTagName("TR");
            this.Rows = 1;
            this.fsMouseOver(this.Trs[this.Rows], 1);
            this.Div.scrollTop = 0;
            this.Dom.title = "";
            this.show_hide = "hide";
            var number;
            if (window.event)
                number = this.divHeight;
            else
                number = this.divHeight + 1;
            if (this.Div.scrollHeight < number)
                this.Div.style.height = "auto";
            else
                this.Div.style.height = this.divHeight + "px";

        }
        else {
            this.fsHideDiv();
        }
        this.inDrawTable = false;

    },
    fsShowDiv: function () {
        this.Div.style.display = 'block';
        this.Div.style.left = getPosition(this.Dom).x + 'px';
        //        if (this.changeTop == null) {
        //            this.fsChangeTop();
        //        } else {
        this.Div.style.top = getPosition(this.Dom).y + this.Dom.offsetHeight + 'px';
        // }
        if (isIE6) {
            for (var i = 0; i < document.getElementsByTagName('select').length; i++) {
                if (this.enterID == 'txtFund_GZ') {
                    document.getElementsByTagName('select')[i].style.display = 'none';
                }
                else {
                    document.getElementsByTagName('select')[i].style.display = 'block';
                }
            }
        }

    },
    fsHideDiv: function () {
        this.Div.style.display = 'none';
        if (isIE6) {
            for (var i = 0; i < document.getElementsByTagName('select').length; i++) {
                document.getElementsByTagName('select')[i].style.display = 'block';
            }
        }
    },
    fsChangeTop: function () {
        var leftmenu = document.getElementById("DIV$Left$Menu");
        if (leftmenu) {
            if (this.Div != null) {
                // this.Div.style.top = getPosition(this.Dom).y + this.Dom.offsetHeight - leftmenu.scrollTop + 'px';

                var curPosition = getPosition(this.Dom).y + this.Dom.offsetHeight - leftmenu.scrollTop;
                this.Div.style.top = parseInt(curPosition) < 124 ? getPosition(this.Dom).y + this.Dom.offsetHeight + 'px' : curPosition + 'px';
            }
        }
        else {
            this.Div.style.top = getPosition(this.Dom).y + this.Dom.offsetHeight + 'px';
        }

    }

}